#!/usr/bin/env bash

function fzf_with {
    file="$(fzf --reverse --query "$2")"
    # in case you press <Esc> and fzf returns zero length of string
    if [ -n "$file" ]; then
        abspath=$(readlink -f "$file")
        dir="${abspath%/*}"
        case "$1" in
            cd) cd "$dir" || return;;
            # *) cd "$dir" && "$1" "$file" || return;;
            *) "$1" "$file" || return;;
        esac
    fi
}

function fcd { fzf_with cd "$@" 
}
function fv {
fzf_with "$EDITOR" "$@"
}
function fz {
fzf_with "$PDFVIEWER" "$@"
}
function fr {
fzf_with "$FILE_MANAGER" "$@"
}
function fs {
fzf_with bluetooth-sendto "$@"
}

# Colorize commands when possible.
alias \
	ls="ls -hN --color=auto --group-directories-first" \
	diff="diff --color=auto" \

# Use neovim for vim if present.
if [ -x "$(command -v nvim)" ]; then
    export EDITOR="nvim"; alias vv='$FILE_MANAGER $HOME/.config/nvim/lua'
else
    export EDITOR="vim"; alias vv='vim $HOME/.vim/vimrc'
fi

function nq {
    if [ $# -eq 0 ]; then
        echo "usage: nq <pattern>"
        return
    fi
    nvim -q <(rg --vimgrep --smart-case "$@")
}

# These common commands are just too long! Abbreviate them.
alias \
    dl="trash" \
    c="clear" \
    e="chmod u+x" \
    g="git" \
    gg="gitui" \
    p="sudo pacman" \
    pt="python" \
    t="tldr" \
    q="exit" \
    r='$FILE_MANAGER' \
    n="newsboat" \
    L="less" \
    .="cd .." \
    ..="cd ../.." \
    lt="du -sh * | sort -h" \
    ka="killall" \
    cd-="cd -" \
    sc="shellcheck" \
    v='$EDITOR' \
    va='$EDITOR $HOME/.config/aliasrc' \
    vb='$EDITOR $HOME/.${SHELL##*/}rc' \
    ve='$EDITOR $HOME/.${SHELL##*/}env' \
    ga="lazygit" \
    mm='$EDITOR -X $HOME/docx/memorandum/Linux.md' \
    jr='$EDITOR $HOME/docx/memorandum/journal.md' \
    wr='cd $HOME/workspace/writing' \
    sshs="ssh root@124.70.3.11" \
    sftps="sftp root@124.70.3.11" \
    neofetch='neofetch --config $HOME/.config/neofetch.conf' \
    myclip='xclip -rmlastnl -sel clipboard' \
    wk="workon" \
    j="jupyter" \
    jt="jupyter nbconvert --to script" \
    jn="jupyter-notebook" \

    # gh="history 0 | grep" \

# aliases that precedes with "setsid -f"
alias \
    deskreen="setsid -f deskreen" \
	za="setsid -f zathura" \
    xournalpp="setsid -f xournalpp" \
    x="setsid -f xournalpp" \
	zathura="setsid -f zathura" \
    rstudio="setsid -f rstudio-bin" \
    spyder="setsid -f spyder" \
    wechat="setsid -f wechat-uos" \
    QQ="setsid -f linuxqq" \
    et="setsid -f et" \
    wps="setsid -f wps" \
    wpp="setsid -f wpp" \
    snd="setsid -f bluetooth-sendto" \
    libreoffice="setsid -f libreoffice" \
    texdoc="setsid -f texdoc" \
    sxiv="setsid -f sxiv" \
    s="setsid -f sxiv" \
    brave="setsid -f brave" \
    gimp="setsid -f gimp" \
    virtualbox="setsid -f virtualbox" \
    mpv="mpv --no-terminal" \
    R="R --quiet --no-save" \

    # brave="setsid -f brave --proxy-server=127.0.0.1:7890" \

# Verbosity and settings that you pretty much just always are going to want.
alias \
	cp="cp -iv" \
	mv="mv -iv" \
    grep="grep --color" \
	mkd="mkdir -pv" \

	# rm="rm -vI" \
